<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>用户管理</title>
    <link rel="stylesheet" href="/css/elementUI.css">
    <script src="/js/vue.js"></script>
    <script src="/js/elementUI.js"></script>
    <script src="/js/axios.js"></script>
    <script src="/js/util.js"></script>
</head>
<body>

<div id="app">

    <el-input v-model="query.loginName" style="width: 200px;" clearable placeholder="关键字查询"></el-input>

    <el-button @click="toAdd">添加</el-button>

    <el-table
            :data="tableData"
            style="width: 100%">
        <el-table-column
                prop="id"
                label="用户id">
        </el-table-column>
        <el-table-column
                prop="loginName"
                label="登录名称">
        </el-table-column>
        <el-table-column
                prop="username"
                label="用户名">
        </el-table-column>
        <el-table-column
                prop="phonenum"
                label="手机号">
        </el-table-column>
        <el-table-column
                label="是否是管理员">
            <template slot-scope="scope">
                {{scope.row.ismanager==0?"管理员":"员工"}}
            </template>
        </el-table-column>
        <el-table-column
                label="是否系统自带">
            <template slot-scope="scope">
                {{scope.row.enable==0?"是":"否"}}
            </template>
        </el-table-column>
        <el-table-column
                label="状态">
            <template slot-scope="scope">
                {{scope.row.status=="0"?"正常":"封禁"}}
            </template>
        </el-table-column>
        <el-table-column
                label="用户名称">
            <template slot-scope="scope">
                {{scope.row.tenantName}}
            </template>
        </el-table-column>
        <el-table-column label="操作">
            <template slot-scope="scope">
                <el-button
                        size="mini"
                        @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                <el-button
                        size="mini"
                        type="danger"
                        @click="handleDelete(scope.$index, scope.row)">删除</el-button>

            </template>
        </el-table-column>
    </el-table>

    <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="query.page"
            :page-sizes="[1, 2, 3, 4]"
            :page-size="query.pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total">
    </el-pagination>


    <el-dialog :title="mode.title" :visible.sync="dialogFormVisible">
        <el-form :model="form">
            <el-form-item label="用户名称" :label-width="formLabelWidth">
                <el-input v-model="form.username" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="密码" :label-width="formLabelWidth">
                <el-input v-model="form.password" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="登录名" :label-width="formLabelWidth">
                <el-input v-model="form.loginName" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="手机号" :label-width="formLabelWidth">
                <el-input v-model="form.phonenum" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="是否管理员" :label-width="formLabelWidth">
                <el-radio v-model="form.ismanager" label="0">管理员</el-radio>
                <el-radio v-model="form.ismanager" label="1">员工</el-radio>
            </el-form-item>
            <el-form-item label="系统自带" :label-width="formLabelWidth">
                <el-radio v-model="form.issystem" label="0">是</el-radio>
                <el-radio v-model="form.issystem" label="1">否</el-radio>
            </el-form-item>

            <el-form-item label="租户" :label-width="formLabelWidth">
                <el-select v-model="form.tenantId" placeholder="请选择">
                    <el-option
                            v-for="item in tenantList"
                            :key="item.id"
                            :label="item.name"
                            :value="item.id">
                    </el-option>
                </el-select>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="updateUser">确 定</el-button>
        </div>
    </el-dialog>

</div>
<script>
    new Vue({
        el:"#app",
        data:{
            tableData:[],
            total:0,
            query:{
                loginName:"",
                page:1,
                pageSize:2
            },

            //表单相关
            dialogFormVisible: false,
            form: {
                username:"",
                password:"",
                loginName:"",
                phonenum:"",
                ismanager:"",
                issystem:"",
                status:"",
                tenantId:"",
            },
            formLabelWidth: '120px',
            mode:{
                title:"",
                url:""
            },
            tenantList:[],

        },
        filters:{
            format(value,arg){
                function dateFormat(date, format) {
                    if (typeof date === "string") {
                        var mts = date.match(/(\/Date\((\d+)\)\/)/);
                        if (mts && mts.length >= 3) {
                            date = parseInt(mts[2]);
                        }
                    }
                    date = new Date(date);
                    if (!date || date.toUTCString() == "Invalid Date") {
                        return "";
                    }
                    var map = {
                        "M": date.getMonth() + 1, //月份
                        "d": date.getDate(), //日
                        "h": date.getHours(), //小时
                        "m": date.getMinutes(), //分
                        "s": date.getSeconds(), //秒
                        "q": Math.floor((date.getMonth() + 3) / 3), //季度
                        "S": date.getMilliseconds() //毫秒
                    };
                    format = format.replace(/([yMdhmsqS])+/g, function (all, t) {
                        var v = map[t];
                        if (v !== undefined) {
                            if (all.length > 1) {
                                v = '0' + v;
                                v = v.substr(v.length - 2);
                            }
                            return v;
                        } else if (t === 'y') {
                            return (date.getFullYear() + '').substr(4 - all.length);
                        }
                        return all;
                    });
                    return format;
                }

                return dateFormat(value,arg);

            }
        },
        watch:{
            query:{
                deep:true,
                handler(){
                    this.initData()
                }
            }
        },
        methods:{
            toAdd(){
                this.dialogFormVisible = true;
                this.mode = {
                    title:"添加用户",
                    url:"/jiyun-user/add"
                }
            },
            updateUser(){
                axios.post(this.mode.url,this.form).then(res=>{
                    this.$message({
                        type: 'success',
                        message: '成功!'
                    });
                    this.dialogFormVisible = false;
                    this.form = {
                        username:"",
                        password:"",
                        loginName:"",
                        phonenum:"",
                        ismanager:"",
                        issystem:"",
                        status:"",
                        tenantId:"",
                    }
                    this.initData();
                })
            },
            handleEdit(index, row) {
                this.dialogFormVisible = true;
                this.form = JSON.parse(JSON.stringify(row));
                this.mode = {
                    title:"修改用户",
                    url:"/jiyun-user/update"
                }
            },
            handleDelete(index, row) {
                this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    axios.post("/jiyun-user/deleteOne?id="+row.id).then(res=>{
                        this.initData();
                        this.$message({
                            type: 'info',
                            message: "删除成功"
                        });
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            handleSizeChange(val) {
                this.query.pageSize = val
            },
            handleCurrentChange(val) {
                this.query.page = val
            },
            initData(){
                axios.post(`/jiyun-user/findPage?page=${this.query.page}&pageSize=${this.query.pageSize}`,this.query).then(res=>{
                    console.log(res.data)
                    this.tableData = res.data.records;
                    this.total = res.data.total;

                    if(this.tableData.length==0&&this.query.page>1){
                        this.query.page--
                    }
                })

            },
            initTenantData(){
                axios.post(`/tenant/findAll`).then(res=>{
                    this.tenantList = res.data;
                })
            }

        },
        created(){
            this.initData()
            this.initTenantData();
        }
    })
</script>

</body>
</html>